package com.telit.android.controller;

import com.telit.android.dto.AppLocationDTO;
import com.telit.android.entity.AppUserLocation;
import com.telit.android.entity.PatrolWorker;
import com.telit.android.service.AppLocationService;
import com.telit.android.service.impl.RemoteServiceProvider;
import com.telit.common.entity.DataReturn;
import com.telit.common.enums.ResultCode;
import com.telit.firecommand.feign.RemoteFirecommandService;
import com.telit.firecommand.model.sysUser.UpdateUserRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;


/**
 * @Auther amy
 * @Describe app端用户定位
 */
@RestController
@RequestMapping("/app-location")
@Api(tags = "app端用户定位")
public class AppUserLocationController {

    @Autowired
    private AppLocationService appLocationService;

    @Autowired
    private RemoteFirecommandService remoteFirecommandService;

    @Autowired
    private RemoteServiceProvider remoteServiceProvider;

    @PostMapping(value = "/upload", produces = "application/json;charset=UTF-8")
    @ApiOperation("上传定位（app端）")
    public DataReturn uploadLocation(@RequestBody AppLocationDTO appLocationDTO) {
        if (appLocationService.uploadLocation(appLocationDTO) <= 0) {
            return DataReturn.failure(ResultCode.OPERATE_ERROR, "上传定位失败");
        }

        //更新sys-user表信息
        remoteFirecommandService.updateUser(UpdateUserRequest.builder()
                .userId(appLocationDTO.getUserId())
                .latitude(appLocationDTO.getLatitude())
                .longitude(appLocationDTO.getLongitude())
                .build());
        //
        //更新gis点位信息
        PatrolWorker patrolWorker = new PatrolWorker();
        patrolWorker.setLatitude(appLocationDTO.getLatitude());
        patrolWorker.setLongitude(appLocationDTO.getLongitude());
        patrolWorker.setUserId(appLocationDTO.getUserId());
        patrolWorker.setPatrolType(1);
        remoteServiceProvider.pushWorkerPosition(Collections.singletonList(patrolWorker));
        return DataReturn.ok();
    }

    /**
     * 查找在线用户信息
     * 在线：四分钟之内有经纬度的上传
     * 离线：四分钟之内无经纬度上传
     *
     * @return 在线用户
     */
    @GetMapping(value = "/online-list", produces = "application/json;charset=UTF-8")
    @ApiOperation("查找在线用户信息")
    public DataReturn<List<AppUserLocation>> onlineList() {
        return DataReturn.ok(appLocationService.onlineList());
    }

}
